package com.basic.business.common.utils.kmUtil;

import com.basic.business.common.constants.KmConstants;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 封装post请求参数
 *
 * @author zhaoxy_jn@163.com
 */
@Slf4j
public class PostParamsUtil {

    private static final String FORMAT = "json";
    private static final String VERSION = "1.0";
    private static final String SIGN_METHOD = "md5";
    private static final String USE_HAS_NEXT = "true";
    private static final String TIME_TYPE = "upd_time";
    private static final String PAGE_SIZE = "200";

    /**
     * 业务请求参数封装
     */
    public static Map<String, String> getBusinessParams(String pageNo, String method, Map<String, String> businessParams) {
        try {
            Map<String, String> params = createBaseParams(method);
            params.put("pageNo", pageNo);
            params.put("pageSize", PAGE_SIZE);
            params.put("timeType", TIME_TYPE);
            params.put("useHasNext", USE_HAS_NEXT);
            params.putAll(businessParams);
            params.put("sign", RopUtil.serverSign(params, KmConstants.APP_SECRET, method));
            return params;
        } catch (Exception e) {
            log.error("【业务请求参数】失败，错误信息--->{}", e.getMessage());
            throw new RuntimeException("业务请求参数封装失败", e);
        }
    }

    /**
     * 刷新token请求参数封装
     */
    public static Map<String, String> getRefreshTokenParams() {
        try {
            Map<String, String> params = createBaseParams(KmConstants.REFRESH_TOKEN_METHOD);
            params.put("refreshToken", KmConstants.REFRESH_TOKEN);
            params.put("sign", RopUtil.serverSign(params, KmConstants.APP_SECRET, KmConstants.REFRESH_TOKEN_METHOD));
            return params;
        } catch (Exception e) {
            throw new RuntimeException("刷新TOKEN请求参数封装失败", e);
        }
    }

    /**
     * 标签请求参数
     */
    public static Map<String, String> getTagParams(String method, Map<String, String> tagParams) {
        try {
            Map<String, String> params = createBaseParams(method);
            params.putAll(tagParams);
            params.put("sign", RopUtil.serverSign(params, KmConstants.APP_SECRET, method));
            return params;
        } catch (Exception e) {
            log.error("【标签请求参数】失败，错误信息--->{}", e.getMessage());
            throw new RuntimeException("标签请求参数封装失败", e);
        }
    }

    /**
     * 创建基础参数
     */
    private static Map<String, String> createBaseParams(String method) {
        Map<String, String> params = Maps.newHashMap();
        params.put("appKey", KmConstants.APP_KEY);
        params.put("format", FORMAT);
        params.put("method", method);
        params.put("session", KmConstants.ACCESS_TOKEN);
        params.put("timestamp", RopUtil.gettime());
        params.put("version", VERSION);
        params.put("sign_method", SIGN_METHOD);
        return params;
    }

}
